<?php

	/**
	 * Defines a class to be used for PostgreSQL databases
	 * 
	 * @author Pablo Santiago Sánchez <phackwer@gmail.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage drivers
	 */

	/**
	 * Defines a class to be used for PostgreSQL databases
	 * 
	 * @package pop
	 * @subpackage drivers
	 */
	class PPgSQLDBDriver extends PGenericDBDriver
	{
		public $schema = 'public';
		public $separator = '.';
		public $driver = 'pgsql';
		
		/**
		 * Returns the last inserted itme id
		 * @param $pdodb
		 * @param $table
		 * @param $idname
		 * @return integer
		 */
		public function getLastId($pdodb, $table, $idname)
		{
			$seq_name = $table."_".$idname."_seq";

			return POPDB::getConnection($pdodb)->lastInsertId($seq_name);
		}
		
		/**
		 * Begin transaction
		 * @param $pdodb
		 * @return void
		 */
		public function beginTransaction($pdodb)
		{
			POPDB::getConnection($pdodb)->beginTransaction();
		}
		
		/**
		 * Commit transaction
		 * @param $pdodb
		 * @return void
		 */
		public function commit($pdodb)
		{
			POPDB::getConnection($pdodb)->commit();
		}
		
		/**
		 * Rollsback transaction
		 * @param $pdodb
		 * @return void
		 */
		public function rollBack($pdodb)
		{
			POPDB::getConnection($pdodb)->rollBack();
		}

		/**
		 * Returns the required string for the createTable method
		 * @return string
		 */
		public function identityField()
		{
			return " serial ";
		}
		
		/**
		 * Creates the required schema to avoid errors on creating tables
		 * @param $schema
		 * @return void
		 */
		public function createSchema($pdodb, $schema)
		{
			//first, check if schema exists			
			$sql    = "select * from pg_namespace where nspname='".$schema."'";
			$this->beginTransaction($pdodb);
			$rs = POPDB::getConnection($pdodb)->query($sql);
			$result = $rs->fetchAll();
			$rs->closeCursor();
			$this->commit($pdodb);
			POPDebug::log($sql);
			$count = 0;
			foreach ($result as $row)
				$count++;

			//if it doesn't, than create it
			if ($count == 0)
			{
				$sql = "create schema ".$schema;
				$this->beginTransaction($pdodb);
				POPDB::getConnection($pdodb)->exec($sql);
				$this->commit($pdodb);
				POPDebug::log($sql);
			}
		}

		/**
		 * Returns the required SQL for field creation
		 * @param $type
		 * @param $defaultval
		 * @param $size
		 * @return void
		 */
		public function fieldCreation($type, $defaultval, $size=null)
		{
			if ($type=="datetime")
				$sql_field = " timestamp ";
			else
				$sql_field = " ".$type." ";

			if ($type=="varchar")
				$sql_field .= "(".$size.") ";

			if (is_null($defaultval))
				$sql_field .= " default null";
			else
			{
				if ($defaultval && $type!="datetime" && $type!="date" && $type!="time" && $type!="varchar" && $type!="text")
					$sql_field .= " default ".$defaultval;
				else if ($type=="varchar")
					$sql_field .= " default '".$defaultval."'";
			}
			
			return $sql_field;
		}

		/**
		 * Alter table
		 * @return void
		 */
		public function alterTable()
		{
		}
		
		/**
		 * Drops a table
		 * @param $pdodb
		 * @param $schema
		 * @param $table
		 * @return void
		 */
		public function dropTable($pdodb, $schema, $table)
		{
			$sql    = "select * from pg_tables where tablename='".$table."' and schemaname='".$schema."'";
			$this->beginTransaction($pdodb);
			$rs = POPDB::getConnection($pdodb)->query($sql);
			$result = $rs->fetchAll();
			$rs->closeCursor();
			$this->commit($pdodb);
			POPDebug::log($sql);
			$sql = "";
			foreach ($result as $row)
				$sql = "drop table ".$schema.".".$table." cascade;\n";
				
			if ($sql)
			{
				$this->beginTransaction($pdodb);
				POPDB::getConnection($pdodb)->exec($sql);
				$this->commit($pdodb);
				POPDebug::log($sql);
			}
		}
	}
	
	/** 
	 * Register the class for the driver
	 */
	POPDBDriverRegistry::addDriver("pgsql", "PPgSQLDBDriver");

?>